package com.web.action.backend;

import java.math.BigDecimal;
import java.util.List;

import com.bo.Order;
import com.bo.OrderData;
import com.bo.Product;
import com.dao.ProductDao;
import com.dao.impl.ProductDaoImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.web.assist.ActionConstant;

public class OrderAction extends ActionSupport{
	private long productId;
    private int prdtCount = 1;

	public int getPrdtCount() {
		return prdtCount;
	}

	public void setPrdtCount(int prdtCount) {
		this.prdtCount = prdtCount;
	}

	public long getProductId() {
		return productId;
	}

	public void setProductId(long productId) {
		this.productId = productId;
	}

	/**
	 * order
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		Object shopcart = ActionContext.getContext().getSession().get(ActionConstant.SHOPPINGCART);
		ProductDao pd = new ProductDaoImpl();
		Product product = pd.loadSimplePrdt(productId);
		if(shopcart == null){
			Order order = new Order();
			
			if(product != null){
				OrderData od = generateOrderData(product);
				order.getData().add(od);
			}
			ActionContext.getContext().getSession().put(ActionConstant.SHOPPINGCART,order);
			
		}else{
			List <OrderData> ordtList = ((Order)shopcart).getData();
			boolean exist = false;
			for(int i =0; i<ordtList.size();i++){
				if(productId == ordtList.get(i).getProductId()){
					exist = true;
					break;
				}
			}
			if(!exist){
				if(product != null){
					OrderData od = generateOrderData(product);
				    ordtList.add(od);
				}
			}
			
		}
		return "shoppingCart";
	}
	
	public String calculatePrice() throws Exception {
		// TODO Auto-generated method stub
		Object shopcart = ActionContext.getContext().getSession().get(ActionConstant.SHOPPINGCART);

		if(shopcart != null){
			List<OrderData> shopCart = ((Order)shopcart).getData();
			for(OrderData od : shopCart){
				if(od.getProductId() == productId){
					od.setTotalPrice(od.getUnitPrice().multiply(new BigDecimal(prdtCount)));
					od.setCount(prdtCount);
					break;
				}
			}
		}
		return "shoppingCart";
	}
	
	public String deleteProduct() throws Exception {
		// TODO Auto-generated method stub
		Object shopcart = ActionContext.getContext().getSession().get(ActionConstant.SHOPPINGCART);
		
		if(shopcart != null){
			List<OrderData> shopCart = ((Order)shopcart).getData();
			for(OrderData od : shopCart){
				if(od.getProductId() == productId){
					shopCart.remove(od);
					break;
				}
			}
		}
		return "shoppingCart";
	}

	private OrderData generateOrderData(Product product) {
		OrderData od = new OrderData();
		od.setCount(1);
		od.setProductId(productId);
		od.setSubject(product.getTitle());
		od.setPrdtPic(product.getMainPic());
		od.setUnitPrice(product.getRealPrice());
		od.setTotalPrice(product.getRealPrice());
		return od;
	}
	
	/**
	 * confirm order
	 * @return
	 * @throws Exception
	 */
	public String confirmOrder() throws Exception{
		System.out.println("confirm");
		return "orderConfirm";
	}
}
